<?php

/**
 * Controller_baseset
 */

class Controller_baseset extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->student = YOU::loadModel('student');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->baseset  = YOU::loadModel('baseset');


	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 学校基础信息列表 **/
	public function actionList()
	{

		$baseset_data  = $this->db->fetchRow("SELECT * FROM `tbl_baseset` ");
		$baseset_data['baseset_introduce'] = nl2br($baseset_data['baseset_introduce']);
		
		// 教师总数
		$row  = $this->db->fetchRow("SELECT count(1) AS count FROM `tbl_staff`");
		$staff_count = (int)$row['count'];

		// 班级总数
		$row  = $this->db->fetchRow("SELECT count(1) AS count FROM `tbl_class_up` WHERE is_now=1");
		$class_count = (int)$row['count'];

		// 学生总数
		$row  = $this->db->fetchRow("SELECT count(1) AS count FROM `tbl_student`");
		$student_count = (int)$row['count'];

		// 年级列表
		$grade_lists = $this->db->fetchAll("SELECT * FROM `tbl_grade_up` WHERE is_now=1");
		// 学校类型
		$baseset_data['baseset_type'] =  $this->etype_ini[$baseset_data['baseset_type']];
	
		$baseset_data['baseset_map']  = base64_decode($baseset_data['baseset_map']);


		$this->tpl->assign("baseset_data", $baseset_data);
		$this->tpl->assign("class_count", $class_count);
		$this->tpl->assign("student_count", $student_count);
		$this->tpl->assign("staff_count", $staff_count);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->display('baseset/baseset.list.tpl.php');


	}


	/** 修改学校基础信息 **/
	public function actionUpdate()
	{

		$baseset_id   = 1;
		$province_id  = (int)$this->post->province_id;
		$city_id	  = (int)$this->post->city_id;


		// 基本
		$baseset_data = $this->db->fetchRow("SELECT * FROM `tbl_baseset` WHERE `baseset_id`='{$baseset_id}'");
		$baseset_data['baseset_map'] = $baseset_data['baseset_map'];

		if (empty($baseset_data)){
			Helper_Client::alert("配置不存在");
		}

		if ($_POST){

			$baseset_name				= $this->post->baseset_name;
			$baseset_english			= $this->post->baseset_english;
			$baseset_ename				= $this->post->baseset_ename;
			$baseset_introduce     		= $this->post->baseset_introduce;
			$baseset_code  	   			= $this->post->baseset_code;
			$baseset_type      			= $this->post->baseset_type;
			$baseset_area      			= $this->post->baseset_area;
			$baseset_province  			= $this->post->baseset_province;
			$baseset_city      			= $this->post->baseset_city;
			$baseset_district  			= $this->post->baseset_district;
			$baseset_street    			= $this->post->baseset_street;
			$baseset_addr    			= $this->post->baseset_addr;
			$baseset_tel       			= (string)$this->post->baseset_tel;
			$baseset_fax      			= (string)$this->post->baseset_fax;
			$baseset_postcode  			= (string)$this->post->baseset_postcode;
			$baseset_url       			= $this->post->baseset_url;
			$baseset_email     			= $this->post->baseset_email;
			$baseset_nation    			= $this->post->baseset_nation;
			$baseset_usearea   			= $this->post->baseset_usearea;
			$baseset_buildarea 			= $this->post->baseset_buildarea;
			$baseset_time  				= $this->post->baseset_time;
			$baseset_content			= $this->post->baseset_content;
			$baseset_map     			= base64_encode(stripslashes($this->post->baseset_map));


			$baseset_gradenum  			= $this->post->baseset_gradenum;
			$baseset_classnum  			= $this->post->baseset_classnum;
			$baseset_studentnum  	    = $this->post->baseset_studentnum;
			$baseset_staffnum  			= $this->post->baseset_staffnum;


			// 写入数据库
			$info = array(

			"baseset_name"				=> $baseset_name,
			"baseset_english"			=> $baseset_english,
			"baseset_ename"				=> $baseset_ename,
			"baseset_introduce"			=> $baseset_introduce,
			"baseset_code"				=> $baseset_code,
			"baseset_type"				=> $baseset_type,
			"baseset_area"				=> $baseset_area,
			"baseset_province"			=> $baseset_province,
			"baseset_city"				=> $baseset_city,
			"baseset_district"			=> $baseset_district,
			"baseset_street"		    => $baseset_street,
			"baseset_addr"				=> $baseset_addr,
			"baseset_tel"				=> $baseset_tel,
			"baseset_fax"				=> $baseset_fax,
			"baseset_postcode"			=> $baseset_postcode,
			"baseset_url"				=> $baseset_url,
			"baseset_email"				=> $baseset_email,
			"baseset_nation"			=> $baseset_nation,
			"baseset_usearea"			=> $baseset_usearea,
			"baseset_buildarea"			=> $baseset_buildarea,
			"baseset_time"				=> $baseset_time,
			"baseset_content"			=> $baseset_content,
			"baseset_gradenum"			=> $baseset_gradenum,
			"baseset_classnum"			=> $baseset_classnum,
			"baseset_studentnum"		=> $baseset_studentnum,
			"baseset_staffnum"			=> $baseset_staffnum,

			"baseset_map"			    => $baseset_map,
			"status"					=> 1,
			"create_time"				=> date("Y-m-d H:i:s"),
			"update_time"				=> "0000-00-00 00:00:00",
			);

			//print_r($info);exit;

			$this->db->do_update("tbl_baseset", $info, "baseset_id='{$baseset_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=baseset.list");
		}

		$this->tpl->assign("baseset_data", $baseset_data);
		$this->tpl->display("baseset/baseset.update.tpl.php");
	}

	/** 平台链接设置 **/
	public function actionSurl()
	{

		$baseset_id   = 1;

		// 基本
		$baseset_data = $this->db->fetchRow("SELECT * FROM `tbl_baseset` WHERE `baseset_id`='{$baseset_id}'");

		if (empty($baseset_data)){
			Helper_Client::alert("配置不存在");
		}

		if ($_POST){

			$baseset_surl				= $this->post->baseset_surl;

			if (empty($baseset_surl)){
				Helper_Client::alert("网址不能为空");
			}

			// 写入数据库
			$info = array(
			"baseset_surl"				=> $baseset_surl,
			);
			$this->db->do_update("tbl_baseset", $info, "baseset_id='{$baseset_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=baseset.surl");
		}

		$this->tpl->assign("baseset_data", $baseset_data);
		$this->tpl->display("baseset/baseset.surl.tpl.php");
	}

	/** 年级数量 **/
	public function actionCountGrade()
	{
		echo $this->model->grade->count();
	}


	/** 班级数量 **/
	public function actionCountClass()
	{

		echo $this->model->class->count();
	}

	/** 教师数量 **/
	public function actionCountStaff()
	{

		echo $this->model->staff->count();
	}

	/** 学生数量 **/
	public function actionCountStudent()
	{
		echo $this->model->student->count();
	}
	/** 设置上传大小 **/
	public function actionUploadSize()
	{	
		
		$baseset_data 		= $this->model->baseset->getrow();
		if ($_POST) {
			$baseset_upload     = $this->post->baseset_upload;
			$info = array(
				"baseset_upload" 	=> $baseset_upload,				
				
				);

				$this->db->do_update("tbl_baseset", $info, "baseset_id='1'");
				Helper_Client::location("设置成功", "index.php?a=baseset.uploadsize");
		}
		
		$this->tpl->assign("baseset_data", $baseset_data);
		$this->tpl->display("baseset/baseset.size.tpl.php");
	}
	
}

?>